package com.xiaomili.halfsugar.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.fragment.CommunityFragment;
import com.xiaomili.halfsugar.fragment.HomeFragment;
import com.xiaomili.halfsugar.fragment.MessageFragment;
import com.xiaomili.halfsugar.fragment.PersonalFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends AppCompatActivity implements OnCheckedChangeListener, OnClickListener {
    private static final String TAG = "MainActivity";
    @InjectView(R.id.rb_bottom_publish)
    RadioButton rbBottomPublish;
    private Activity mActivity = this;

    private List<Fragment> mFragments;

    private int currentIndex;
    private int previousIndex;
    //定义消息的常量
    private static final int MSG = 0X00;
    private static final long MSG_DUTATION = (long) (1.5 * 1000);
    /**
     * 用来统计后退键点击的次数
     */
    private int times;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case MSG:
                    times = 0;
                    break;
            }
            return false;
        }
    });

    @InjectView(R.id.rg_bottom_container)
    RadioGroup rgBottomContainer;
    private FragmentTransaction mTransation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        initFragment();
        showFragment(currentIndex, previousIndex);
        ((RadioButton) rgBottomContainer.getChildAt(previousIndex)).setChecked(true);
        setListener();
    }


    //初始化碎片
    private void initFragment() {
        mFragments = new ArrayList<>();
        mFragments.add(new HomeFragment());
        mFragments.add(new CommunityFragment());
        mFragments.add(new MessageFragment());
        mFragments.add(new PersonalFragment());
    }

    //设置监听事件
    private void setListener() {
        rgBottomContainer.setOnCheckedChangeListener(this);
        rbBottomPublish.setOnClickListener(this);
    }

    //碎片的显示方法
    private void showFragment(int currentIndex, int previousIndex) {
        mTransation = getSupportFragmentManager().beginTransaction();
        Fragment showFragment = mFragments.get(currentIndex);
        Fragment hideFragment = mFragments.get(previousIndex);
        if (!showFragment.isAdded()) {
            mTransation.add(R.id.fl_container, showFragment);
        }
        if (previousIndex == currentIndex) {
            mTransation.show(showFragment);
        } else {
            mTransation.show(showFragment).hide(hideFragment);
        }
        mTransation.commit();

    }

    //跳转MainActivity界面的方法
    public static void toMainactivity(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);

    }

    /**
     * RadioGroup的点击事件
     *
     * @param group
     * @param checkedId
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        RadioButton rb = (RadioButton) group.findViewById(checkedId);
        rb.setChecked(true);
        int indx = group.indexOfChild(rb);
        if (indx > 2) {
            indx--;
        }
        currentIndex = indx;
        showFragment(currentIndex, previousIndex);
        previousIndex = currentIndex;
    }


    //中间RadioButton点击事件
    @Override
    public void onClick(View v) {
        PublishActivity.toPublishActivity(mActivity);
        overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim);
    }

    @Override
    public void onBackPressed() {
        times++;
        if (times == 1) {
            Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
            handler.sendEmptyMessageDelayed(MSG, MSG_DUTATION);
        } else {
            finishAffinity();
        }
    }


}
